// this file aggregates the kid-parent-wave file to the parent-wave level and defines events
clear
clear matrix
clear mata

set maxvar 30000

// load kid_parent_wave file
use data/constructed_data/kid_parent_wave_all.dta, clear
local diag rhlthlm rhibp rdiab rcancr rlung rheart rstrok rpsych rarthr
local hscale radla riadla rcesd

// generate hhld level variables
// rshlt - 5 is poor, 1 is excellent
gen bad_health = cond(rshlt > 3, 1, 0)
replace bad_health = . if missing(rshlt)
gen has_disability = cond(radla > 0 | riadla > 0, 1, 0)
replace has_disability = cond(missing(radla) | missing(riadla), ., has_disability)
egen any_bad_health = max(bad_health), by(hhid wave)

egen any_disability = max(has_disability), by(hhid wave)
egen oldest_age = max(ragey_e), by(hhid wave)



egen any_medicaid = max(rgovmd), by(hhid wave)
egen any_medicare = max(rgovmr), by(hhid wave)
egen any_va = max(rgovva), by(hhid wave)

gen ris_nonwhite = cond(raracem != 1, 1, 0)
replace ris_nonwhite = . if mi(raracem)

egen any_nonwhite = max(ris_nonwhite), by(hhid wave)
egen highest_ed_yr = max(raedyrs), by(hhid wave)
egen any_live_nrshm = max(rnhmliv), by(hhid wave)

gen rec_ssi = rdstat == 22 | rdstat == 2
egen any_ssi = max(rec_ssi), by(hhid wave)

// is the parent male?
gen is_male = (ragender == 1)
gen is_female = (ragender == 2)

//make a list of parent-kid-wave level events;
gen rcardiac = cond(rstrok_dummy == 1 | rheart == 1, 1, 0)
replace rcardiac = . if mi(rstrok_dummy) | mi(rheart)


//hhld structure
bysort hhidpn wave: gen nkids2 = _N
// single and living alone
gen live_alone = hcpl == 0 & hhhres == 1

// living with just spouse
gen live_spouse = hcpl == 1 & hhhres == 2

// living with another person
gen live_other_single = hcpl == 0 & hhhres > 1

gen live_other_couple = hcpl == 1 & hhhres > 2
//make hhid into a group
egen hhid_group = group(hhid)

// transfers to helping children
gen real_ktcamt_help = khelpr*real_ktcamt
gen real_ktcamt_lvtenmi = klvtenmi*real_ktcamt
gen real_kfcamt_help = khelpr*real_kfcamt
gen real_kfcamt_lvtenmi = klvtenmi*real_kfcamt
gen real_ktcamt_either = 0
replace real_ktcamt_either = real_ktcamt if khelpr == 1 | klvtenmi == 1

gen real_ktcamt_female = (kgender == 2)*real_ktcamt
gen real_ktcamt_male = (kgender == 1)* real_ktcamt


/////// calculate gender specific helping variables
// female and male helpers
gen male_helpr = (kgender == 1)*khelpr
gen female_helpr = (kgender == 2)*khelpr

// whether spouse of kids helps 
gen malesp_helpr = (kpgender == 1)*kphelpr
gen femalesp_helpr = (kpgender == 2)*kphelpr

// help hours
gen male_help_hrs = (kgender == 1)*khlphrs
gen female_help_hrs = (kgender == 2)*khlphrs

//hours spouse of male and female kids help hours
gen malesp_help_hrs = (kpgender == 1)*kphlphrs
gen femalesp_help_hrs = (kpgender == 2)*kphlphrs

// whether helper got paid
gen male_help_paid = (kgender == 1)*khlppaid
gen female_help_paid = (kgender == 2)*khlppaid

// financial transfers
gen real_kfcamt_male = (kgender == 1)* real_kfcamt
gen real_kfcamt_female = (kgender == 2)*real_kfcamt

//coresidence
gen male_coresid = (kgender == 1)*(coresid == 1)
gen female_coresid = (kgender == 2)*(coresid == 1)

// lives within 10 miles
gen male_lvtenmi = (kgender == 1)*(klvtenmi == 1)
gen female_lvtenmi = (kgender == 2)*(klvtenmi == 1)

// labor supply or hours of employment
gen male_ft = (kgender == 1)*(kwork == 2)
gen male_pt = (kgender == 1)*(kwork == 1)
gen female_ft = (kgender == 1)*(kwork == 2)
gen female_pt = (kgender == 1)*(kwork == 1)

//close and faraway helpers
gen near_helpr = (coresid == 1 | klvtenmi == 1)* khelpr
replace near_helpr = . if mi(kresd) | mi(klvtenmi)

gen far_helpr = (coresid == 0 & klvtenmi == 0)*khelpr
replace far_helpr = . if mi(kresd) | mi(klvtenmi)

gen kmale = kgender == 1
gen kfemale = kgender == 2


// cost of hours helping
// median of income bracket
// gen kincmed = (kincmax - kincmin)/2
// value of hours helped -- assuming full time work and multiplying by min of bracket
// kwork is 1 if parttime and 2 if full time
// for several waves they don't reask income if they're  repeat respondent (.y)
replace kincmin = kincmin[_n-1] if kincmin == .y
replace kincmin = kincmin[_n-2] if kincmin == .
gen khlpval = khlphrs*kincmin/(1000*kwork) if kwork != 0
replace khlpval = 0 if kwork == 0 & !mi(khlphrs)


// collapse to respondent level
collapse (mean) harles hatran habsns haira hastck hacd habond haothr hadebt hahous hamort hahmln hahoub hamrtb hanethb hafrles haftran hafbsns hafira hafstck hafcd hafbond hafothr hafdebt hafhous hafmort ///
hafhmln hafhoub hafmrtb hiothr khlpval khlphrs sgender riwstat any_medicaid any_medicare any_va rlbrf ssayret rsayret slbrf hatotf hatotfc hatotnc hatotn hatota shhidpn sbyear rabyear rfinr sfinr nkids2 ripena risret rigxfr sipena sisret sigxfr hhid_group kapick is_male is_female coresid hacohort real_hoopmd ris_nonwhite bad_health rcardiac has_disability any_ssi rdstat rec_ssi hcpl live_alone live_spouse live_other_single live_other_couple any_bad_health any_disability oldest_age any_nonwhite highest_ed_yr any_live_nrshm real_rearn real_hitot  rmrct real_hatota raracem raedyrs rshlt rmstat ragender ragey_e rretemp sretemp jl_* rhosp rhlthlm rhibp rdiab rcancr rlung rheart rstrok_dummy rpsych rarthr radla riadla rcesd avg_ktcamt= real_ktcamt avg_real_ktcamt = real_ktcamt avg_real_ktcamt_pos = real_ktcamt_pos avg_real_kfcamt = real_kfcamt avg_real_kfcamt_pos = real_kfcamt_pos avg_ktcntran = ktcntran rhltc hatotac either_parent_has_poorhealth ris_unmar_fem rwthh rjcten rjlastm rjlasty riwendm riwendy rliv* rbeq* rwork* swork* rpnhm* real_roopmd real_soopmd rnrshom rdoctor rhomcar rmemry ralzhe rdemen hchild ///
(sum) ktcntran ktctran kfcflg tot_real_ktcamt = real_ktcamt tot_real_ktcamt_pos = real_ktcamt_pos kfcntran tot_real_kfcamt = real_kfcamt tot_real_kfcamt_pos = real_kfcamt_pos nk_male = kmale nk_female = kfemale ///
nk_hlpadl = khlpadl nk_hlpiadl = khlpiadl nk_hlpfin = khlpfin nk_hlpfut = khlpfut nk_hlpchr = khlpchr nk_hltcst = khltcst nk_helpr = khelpr nk_near_helpr = near_helpr nk_far_helpr = far_helpr nk_male_helpr = male_helpr nk_female_helpr = female_helpr nk_hlpdays = khlpdays nk_hlppaid = khlppaid ///
nk_contyr = kcontyr nk_lvtenmi = klvtenmi tr_ktcamt_help = real_ktcamt_help tr_ktcamt_female = real_ktcamt_female tr_kfcamt_female = real_kfcamt_female tr_ktcamt_male = real_ktcamt_male tr_kfcamt_male = real_kfcamt_male tr_ktcamt_lvtenmi = real_ktcamt_lvtenmi tr_kfcamt_help = real_kfcamt_help tr_kfcamt_lvtenmi = real_kfcamt_lvtenmi tr_ktcamt_either = real_ktcamt_either ///
tot_hlphrs_kids = khlphrs tot_hlphrs_sp = kphlphrs tot_hlpdays = khlpdays tot_hlpval = khlpval tot_female_help = female_helpr tot_male_help = male_helpr tot_femalesp_help = femalesp_helpr tot_malesp_help = malesp_helpr ///
tot_female_hlphrs = female_help_hrs tot_male_hlphrs = male_help_hrs tot_femalesp_hlphrs = femalesp_help_hrs tot_malesp_hlphrs = malesp_help_hrs tot_male_coresid = male_coresid ///
tot_female_coresid = female_coresid tot_female_lvtenmi = female_lvtenmi tot_male_lvtenmi = male_lvtenmi ///
tot_female_ft = female_ft tot_female_pt = female_pt tot_male_ft = male_ft tot_male_pt = male_pt ///
nkmale = kmale nkfemale = kfemale ///
(count) n_ktcntran=ktcntran n_kfcntran = kfcntran n_rwork = rwork (max) inw any_pk_tran = ktcntran any_kp_tran = kfcntran any_khelpr = khelpr  , by(hhidpn wave)

////////////////
bys hhid_group wave: gen num_parents = _N
drop if mi(hhid_group)

gen tot_hlphrs = tot_hlphrs_kids + tot_hlphrs_sp

// any help
gen any_male_help = tot_male_help > 0
gen any_female_help = tot_female_help > 0

gen any_malesp_help = tot_malesp_help > 0
gen any_femalesp_help = tot_femalesp_help > 0

// frac help from a specific gender (includes spouse when applicable)
gen frac_male_hlphrs = (tot_male_hlphrs + tot_malesp_hlphrs)/tot_hlphrs
gen frac_female_hlphrs = (tot_female_hlphrs + tot_femalesp_hlphrs)/tot_hlphrs

// any coresidence
gen any_male_coresid = tot_male_coresid > 0
gen any_female_coresid = tot_female_coresid > 0

// full and part time
gen frac_ktcamt_help = tr_ktcamt_help/tot_real_ktcamt if tot_real_ktcamt > 0
gen frac_ktcamt_lvtenmi = tr_ktcamt_lvtenmi/tot_real_ktcamt if tot_real_ktcamt > 0

gen frac_kfcamt_help = tr_kfcamt_help/tot_real_kfcamt if tot_real_kfcamt > 0
gen frac_kfcamt_lvtenmi = tr_kfcamt_lvtenmi/tot_real_kfcamt if tot_real_kfcamt > 0

gen frac_ktcamt_either = tr_ktcamt_either/tot_real_ktcamt if tot_real_ktcamt > 0

gen frac_ktcamt_lvtenmi_avg = (tr_ktcamt_lvtenmi/nk_lvtenmi)/avg_real_ktcamt
gen frac_ktcamt_help_avg = (tr_ktcamt_help/nk_helpr)/avg_real_ktcamt

gen frac_kfcamt_lvtenmi_avg = (tr_kfcamt_lvtenmi/nk_lvtenmi)/avg_real_kfcamt
gen frac_kfcamt_help_avg = (tr_kfcamt_help/nk_helpr)/avg_real_kfcamt

//female
gen frac_kfcamt_female_avg = (tr_kfcamt_female/nk_female)/avg_real_kfcamt
gen frac_ktcamt_female_avg = (tr_ktcamt_female/nk_female)/avg_real_ktcamt

gen frac_kfcamt_female = (tr_kfcamt_female)/tot_real_kfcamt
gen frac_ktcamt_female = (tr_ktcamt_female)/tot_real_ktcamt

gen any_tran_pk = cond(ktcntran > 0, 1, 0)
replace any_tran_pk = . if n_ktcntran == 0 // n_ktcntran is count of non-missing obs from collapse; needed because collapse sum(all missings) = 0 not .
replace tot_real_ktcamt_pos = . if tot_real_ktcamt_pos == 0

gen any_tran_kp = cond(kfcntran > 0, 1, 0)
replace any_tran_kp = . if n_kfcntran == 0
replace tot_real_kfcamt_pos = . if tot_real_kfcamt_pos == 0

//replace coresid = cond(coresid > 0, 1, 0)


// percapita wealth and icome
gen real_hitot_pc =  real_hitot/(1 + hcpl)
gen real_hatota_pc =  real_hatota/(1 + hcpl)


//# of siblings --assume it's # of kids in the household - 1
gen knsibs = nkids2 - 1

gen real_hitot_ten = real_hitot/10000
gen real_hatota_ten = real_hatota/10000

// make the names shorter
ren any_tran_pk at_pk
ren any_tran_kp at_kp
ren tot_real_ktcamt tr_ktcamt
ren tot_real_kfcamt tr_kfcamt


// set new panel structure
xtset hhidpn wave, generic

// create adult life events
gen rgrad = cond(l.raedyrs < raedyrs & raedyrs == 16, 1, 0)
replace rgrad = . if mi(raedyrs) | mi(l.raedyrs)

gen rgrad_mc = cond(l.raedyrs != 16 & raedyrs == 16, 1, 0)
replace rgrad = . if mi(raedyrs) | mi(l.raedyrs)

// got married  -- includes partneships and absent spouses?
gen rmarriage = cond(l.rmstat > 3 & rmstat <= 3, 1, 0)
replace rmarriage = . if mi(l.rmstat) | mi(rmstat)

//bought a home  -- hafhous == 6
gen rbought_home = cond(l.hafhous == 6 & hafhous < 6, 1, 0)
replace rbought_home = . if mi(l.hafhous) | mi(hafhous) | hafhous > 6 | l.hafhous > 6

gen rownhm = cond(hafhous < 6, 1, 0)
replace rownhm = . if mi(hafhous)

//had a child
gen rhad_child = cond(l.nkids2 < nkids2 , 1, 0)
replace rhad_child = . if mi(l.nkids2) | mi(nkids2)

//find job
gen rfind_job = cond(l.rwork == 0 & rwork == 1, 1, 0)
replace rfind_job = . if mi(l.rwork) | mi(rwork)

// ***job loss***

// respondent job loss
//lost job
gen rlost_job = cond(l.rwork == 1 & rwork == 0, 1, 0)
replace rlost_job = . if mi(l.rwork) | mi(rwork)

// spousal job loss
//lost job
gen slost_job = cond(l.swork == 1 & swork == 0, 1, 0)
replace slost_job = . if mi(l.swork) | mi(swork)

// why did you lose  job?
// job loss among older workers in HRS in lit folder


// head of household job loss

// any job loss
gen alost_job = inlist(1, rlost_job, slost_job)
replace alost_job = . if mi(rlost_job) | mi(slost_job)

// ***Separation***
// mstat codes:
// 1: married
// 2: married, spouse absent
// 3: partnered
// 4: separated
// 5: divorced
// 6: Separated/divorced
// 7: Widowed
// 8: Never married

// widowed
gen rwidowed = rmstat == 7  & l.rmstat < 3
replace rwidowed = . if mi(rmstat)

// divorce
gen rdivorced = (rmstat == 5 | rmstat == 6 | rmstat == 4) & l.rmstat < 3
replace rdivorced = . if mi(rmstat)

// any marriage end
gen rmarriage_end2 = rmstat > 3
replace rmarriage_end2 = . if mi(rmstat)

// gen rmarriage_end = cond(l.rmstat <= 3 & rmstat > 3, 1, 0)
// replace rmarriage_end = . if mi(l.rmstat) | mi(rmstat)

gen ris_married = cond(rmstat <= 3, 1, 0)
replace ris_married = . if mi(rmstat)

//lost home
gen rlost_home = cond(l.hafhous < 6 & hafhous == 6, 1, 0)
replace rlost_home = . if mi(l.hafhous) | mi(hafhous) | hafhous > 6 | l.hafhous > 6

// retired (both semi and full)
// ***Retirement***

// rretemp codes:
//	no retire empstat
//  only retire empstat
//  retire plus other empstat

// respondent full retirement
gen rret = (rretemp == 1 & l.rretemp != 1)
replace rret = . if mi(rretemp)

// spousal retirement
gen sret = (sretemp == 1 & l.sretemp != 1)
replace sret = . if mi(sretemp)

// any retirement
gen aret = inlist(1, rretemp, sretemp)
replace aret = . if mi(rretemp) | mi(sretemp)

// head of household retirement

//is retired
gen ris_retired = cond(rretemp != 0 & l.rretemp == 0, 1, 0)
replace ris_retired = . if mi(rretemp)

// unretire
gen runret = cond(l.rretemp != 0 & rretemp == 0, 1, 0)
replace rret = . if mi(l.rretemp) | mi(rretemp)

// negative health shock
gen rneg_health = cond(rhltc > 3, 1, 0)
replace rneg_health = . if mi(rhltc)

//pos health shock
gen rpos_health = cond(rhltc < 3, 1, 0)
replace rpos_health = . if mi(rhltc)

// negative wealth shock
gen rneg_wealth = cond(hatotac/l.hatota < -.25, 1, 0) if !mi(l.hatota) & l.hatota > 0
replace rneg_wealth = cond(hatotac/l.hatota > .25, 1, 0) if !mi(l.hatota) & l.hatota < 0

gen rneg_wealth_50 = cond(hatotac/l.hatota < -.50, 1, 0) if !mi(l.hatota) & l.hatota > 0
replace rneg_wealth_50 = cond(hatotac/l.hatota > .50, 1, 0) if !mi(l.hatota) & l.hatota < 0

gen rneg_wealth_75 = cond(hatotac/l.hatota < -.75, 1, 0) if !mi(l.hatota) & l.hatota > 0
replace rneg_wealth_50 = cond(hatotac/l.hatota > .75, 1, 0) if !mi(l.hatota) & l.hatota < 0

gen rneg_wealth_100 = cond(hatotac/l.hatota < -1, 1, 0) if !mi(l.hatota) & l.hatota > 0
replace rneg_wealth_100 = cond(hatotac/l.hatota > 1, 1, 0) if !mi(l.hatota) & l.hatota < 0


// shock: change in net assets (excluding second home) is greater than 25% (wealth is hatota)
// gen dwealth_hatota = wealth - l.wealth
sort hhidpn wave
gen wshock_wealth = hatotac/l.hatota <= -.25 if !mi(l.hatota) & l.hatota > 0
replace wshock_wealth = hatotac/l.hatota >= .25 if !mi(l.hatota) & l.hatota < 0

// shock: change in non-house wealth
gen wshock_nonhouse = hatotnc/l.hatotn <= -.25 if !mi(l.hatotn) & l.hatotn > 0
replace wshock_nonhouse = hatotnc/l.hatotn >= .25 if !mi(l.hatotn) & l.hatotn < 0

// shock: change in non-house financial wealth (excludes physical assets like cars and realestate as well as IRAs) measure of liquid assets
gen wshock_nhfin = hatotfc/l.hatotf <= -.25 if !mi(l.hatotf) & l.hatotf > 0
replace wshock_nhfin = hatotfc/l.hatotf >= .25 if !mi(l.hatotf) & l.hatotf < 0

gen any_hcplchange = hcpl != l.hcpl if !mi(l.hcpl)
gen any_nrshmchange = any_live_nrshm == 1 & l.any_live_nrshm == 0
gen wshock_nohhstr = rneg_wealth
replace wshock_nohhstr = 0 if any_hcplchange == 1 | any_nrshmchange == 1



//positive wealth shock
gen rpos_wealth = cond(hatotac/l.hatota > .25, 1, 0)

//any event -- count them up first because rowmax doesn't ignore missings
egen rnum_event = rowtotal(rgrad rmarriage rbought_home rhad_child rfind_job rlost_job rmarriage_end rlost_home rret runret rneg_health rpos_health rneg_wealth rpos_wealth )
gen rany_event = cond(rnum_event > 0 , 1, 0)

// specific health changes
local diag rhlthlm rhibp rdiab rcancr rlung rheart rstrok_dummy rpsych rarthr
foreach d in `diag' {
	gen `d'_new = cond(`d' == 1 & l.`d' == 0, 1, 0)
	replace `d'_new = . if mi(`d') | mi(l.`d')
}

// scale changes
local hscale radla riadla rcesd
foreach s in `hscale' {
	gen `s'_worse = cond(`s' > l.`s', 1, 0)
	replace `s'_worse = . if mi(`s') | mi(l.`s')
}

// go from 3 to 4 and 4 to 5 on health scale

gen health3to4 = cond(rshlt == 4 & l.rshlt == 3, 1, 0)

gen health4to5 = cond(rshlt == 5 & l.rshlt == 4, 1, 0)

gen health3to5 = cond(rshlt == 5 & l.rshlt <= 3, 1, 0)

replace nk_hlpchr = . if wave < 3 | wave > 6
replace nk_hlpadl = . if wave < 3
replace nk_hlpiadl = . if wave < 3
replace nk_hlpfin = . if wave < 3

// make indicators for helping behaviors
egen any_phys_help = rowtotal(nk_hlpadl nk_hlpiadl nk_hlpchr nk_hlpfin)
replace any_phys_help = any_phys_help > 0

egen any_fin_help = rowtotal(nk_hltcst at_kp)
replace any_fin_help  = any_fin_help > 0

egen any_livten = rowtotal(nk_lvtenmi coresid)
replace any_livten  = any_livten> 0

gen any_helpr = cond(nk_helpr > 0, 1, 0)
gen any_male_helpr = cond(nk_male_helpr > 0, 1, 0)
gen any_female_helpr = cond(nk_female_helpr > 0, 1, 0)

gen any_far_helpr = cond(nk_far_helpr > 0, 1, 0)
gen any_near_helpr = cond(nk_near_helpr > 0, 1, 0)


//create memory event
gen rmemory = rmemry == 1 | ralzhe == 1 | rdemen == 1
replace rmemory = . if wave < 4
replace rmemory = . if wave >=4 & wave<10 & mi(rmemry)
replace rmemory = . if wave >= 10 & (mi(ralzhe) | mi(rdemen))


local  pk_events rhomcar rlost_job rret rdivorced rwidowed health3to5 rhosp rcardiac rheart rcancr rstrok_dummy rmemory has_disability rhlthlm jl_reason jl_invol jl_badhealth rhomcar
// loop through them to create HH-wave level events
foreach e in `pk_events'{
	cap drop *_`e'
//  generate flag for any parents missing a cod
//	 count the number of nonmissings
	egen nomi_`e' = count(`e'), by(hhid_group wave)
//	set missing flag to 1 if number of nonmissings doesn't match group #
	gen anymi_`e'   = nomi_`e' != num_parents

//	total # parent events
	egen total_`e' = sum(`e'), by(hhid_group wave)

//	any parent event
/* note: this is  0 if missing both because total is 0 because both missing < 0 */
	/* assumption is that if you're in the wave but missing job loss info, that means you didn't lose your job */
	gen any_`e' = cond(total_`e' > 0, 1, 0)

//	male parent event
	gen is_male_`e' = is_male*`e'
	egen male_`e' = sum(is_male_`e') if !mi(is_male_`e'), by(hhid_group wave)

	gen is_female_`e' = is_female*`e'
	egen female_`e' = sum(is_female_`e') if !mi(is_female_`e'), by(hhid_group wave)

//	total is undefined if any parents are missing
	replace total_`e' = . if anymi_`e' == 1

	drop is_male_`e'
	drop is_female_`e'
	drop nomi_`e'
	drop anymi_`e'
}

egen cohortXwave = group(hacohort wave)

rename has_disability disab
rename any_has_disability any_disab
rename male_has_disability male_disab

// anyone in household working
// rwork and swork are responses to "Are you currently working for pay?"
gen anywork = (rwork == 1 | swork == 1)


save data/constructed_data/hrs_analysis_respondents.dta, replace
